<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/4/18
 * Time: 20:11
 */
namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Email;
use app\common\library\Page;
use think\Db;

/**
 * 问答留言控制器
 * Class Quesans
 * @package app\index\controller
 */
class Quesans extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    /**
     * 问答留言首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):6;
        $keywords=!empty($this->request->param('keywords'))?trim($this->request->param('keywords')):0;

        //根据当前控制器标识查询顶级导航信息
        $pageInfo=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name,title,keywords,description')->find();

        $where='1=1';
        if(!empty($keywords)){
            $where.=' and content like "%'.$keywords.'%"';
        }

        $start=($cpage-1)*$total;
        $url='/quesans/'.$keywords;

        //所有问题
        $problemList=db('problem')->where($where)->order('id desc')->field('id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime')->limit($start,$total)->select();
        if(!empty($problemList)){
            foreach ($problemList as &$v){
                $v['answerCount']=db('answer')->where('problem_id='.$v['id'])->count();
                if(!empty($v['answerCount'])) {
                    $v['answer']=db('answer')->where('problem_id='.$v['id'])->field('id,user_id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime')->find();
                    $v['answer']['user']=db('user')->where('id='.$v['answer']['user_id'])->field('id,nickname,avatar,joinip')->find();
                    $v['answer']['commentCount']=db('comment')->where('answer_id='.$v['answer']['id'])->count();
                }
            }
            unset($v);
            $count=db('problem')->where($where)->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('problemList',$problemList);
            $this->assign('show',$show);
        }

        //热门问题
        $hotProblemList=db('problem')->order('is_hot desc,id desc')->field('id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime')->limit(6)->select();

        $this->assign('pageInfo',$pageInfo);
        $this->assign('hotProblemList',$hotProblemList);
        $this->assign('keywords',$keywords);
        return $this->view->fetch();
    }

    public function detail(){
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;

        //根据当前控制器标识查询顶级导航信息
        $pageInfo=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name,title,keywords,description')->find();

        $problem=db('problem')->where('id='.$id)->field('id,content')->find();
        if(!empty($problem)){
            $problem['answerCount']=db('answer')->where('problem_id='.$problem['id'])->count();
            $problem['answerList']=db('answer')->where('problem_id='.$problem['id'])->field('id,user_id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime')->select();
            if(!empty($problem['answerList'])){
                foreach ($problem['answerList'] as &$v){
                    $v['user']=db('user')->where('id='.$v['user_id'])->field('id,nickname,avatar,joinip')->find();
                    $v['commentCount']=db('comment')->where('answer_id='.$v['id'])->count();
                    if(!empty($v['commentCount'])){
                        $v['commentList']=db('comment')->where('answer_id='.$v['id'])->field('id,user_id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime')->select();
                        if(!empty($v['commentList'])){
                            foreach ($v['commentList'] as &$v1){
                                $v1['user']=db('user')->where('id='.$v1['user_id'])->field('id,nickname,avatar,joinip')->find();
                            }
                            unset($v1);
                        }
                    }
                }
                unset($v);
            }
        }

        //热门问题
        $hotProblemList=db('problem')->order('is_hot desc,id desc')->field('id,content,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime,user_id')->limit(6)->select();
        if(!empty($hotProblemList)){
            foreach ($hotProblemList as &$v2){
                $v2['user']=db('user')->where('id='.$v2['user_id'])->field('id,nickname,avatar,joinip')->find();
                $v2['answerCount']=db('answer')->where('problem_id='.$v2['id'])->count();
                $v2['answer']=db('answer')->where('problem_id='.$v2['id'])->order('id desc')->field('content')->find();
            }
            unset($v2);
        }

        $this->assign('pageInfo',$pageInfo);
        $this->assign('problem',$problem);
        $this->assign('hotProblemList',$hotProblemList);
        return $this->view->fetch();
    }

}